﻿using System;
using System.Windows.Forms;
using Regedit;
using System.Collections.ObjectModel;
using ShpViewer;

namespace ShpViewerGui
{
    public partial class ShpViewerGui : Form
    {
        private float x1;
        private float y1;
        private bool mouseDown = false;
        private bool ctrlDown = false;
        private float scaleX;
        private float scaleY;
        private ShapeFileForView shapeFileForView;

        public ShpViewerGui()
        {
            InitializeComponent();
            this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Shape Files|*.shp";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                cbxFileName.Text = openFileDialog.FileName;
            }
            openFileDialog.Dispose();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (cbxFileName.Text.Trim().Length <= 0)
            {
                MessageBox.Show("The file is null.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            shapeFileForView = new ShapeFileForView(cbxFileName.Text.Trim());
            shapeFileForView.Read();
            scaleX = 1;
            scaleY = 1;
            pictureBox1.Image = shapeFileForView.Render(pictureBox1.Width, pictureBox1.Height, scaleX, scaleY, pictureBox1.Width / 2, pictureBox1.Height / 2);
            btnRoomIn.Enabled = true;
            btnRoomOut.Enabled = true;

            // Write the folder name  and file name to regedit. 
            RegeditReadAndWrite regeditReadAndWrite = new RegeditReadAndWrite(10);
            regeditReadAndWrite.WriteRegedit("ViewFile", cbxFileName.Text.Trim());
            initializationDropFile();
        }

        public void initializationDropFile()
        {
            // Read the folder name  and file name to regedit. 
            RegeditReadAndWrite regeditReadAndWrite = new RegeditReadAndWrite(10);
            Collection<string> Files = regeditReadAndWrite.ReadRegedit("ViewFile");
            cbxFileName.Items.Clear();
            if (Files != null)
            {
                foreach (string item in Files)
                {
                    cbxFileName.Items.Add(item);
                }
                cbxFileName.SelectedIndex = 0;
            }
        }

        private void ShpViewer_Load(object sender, EventArgs e)
        {
            initializationDropFile();
            btnRoomIn.Enabled = false;
            btnRoomOut.Enabled = false;
        }

        private void btnRoomIn_Click(object sender, EventArgs e)
        {
            scaleY = scaleY > 0.2 ? (float)(scaleY - 0.1) : scaleY;
            scaleX = scaleX > 0.2 ? (float)(scaleX - 0.1) : scaleY;
            ShowMap();
        }

        private void btnRoomOut_Click(object sender, EventArgs e)
        {
            scaleY = (float)(scaleY + 0.1);
            scaleX = (float)(scaleX + 0.1);
            ShowMap();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            x1 = e.X;
            y1 = e.Y;

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mouseDown == true && ctrlDown == false)
            {
                mouseDown = false;
                pictureBox1.Image = shapeFileForView.Render(pictureBox1.Width, pictureBox1.Height, x1 - e.X, y1 - e.Y);
            }
            if (mouseDown == true && ctrlDown == true)
            {
                int centerX = (int)((x1 + e.X) * 0.5);
                int centerY = (int)((y1 +e.Y)*0.5);
                scaleX = scaleX + 10;
                scaleY = scaleY + 10;
              //  pictureBox1.Image = shapeFileForView.Render(pictureBox1.Width, pictureBox1.Height, scaleX, scaleY, centerX - pictureBox1.Left, centerY - pictureBox1.Top);
            }
        }

        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                scaleY = (float)(scaleY + 0.1);
                scaleX = (float)(scaleX + 0.1);
            }
            else
            {
                scaleY = scaleY > 0.2 ? (float)(scaleY - 0.1) : scaleY;
                scaleX = scaleX > 0.2 ? (float)(scaleX - 0.1) : scaleY;
            }
            // The e.x and e.y on the form location.
            pictureBox1.Image = shapeFileForView.Render(pictureBox1.Width, pictureBox1.Height, scaleX, scaleY, e.X - pictureBox1.Left, e.Y - pictureBox1.Top);
        }

        public void ShowMap()
        {
            pictureBox1.Image = shapeFileForView.Render(pictureBox1.Width, pictureBox1.Height, scaleX, scaleY, pictureBox1.Width / 2, pictureBox1.Height / 2);
        }

        private void ShpViewerGui_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                ctrlDown = true;
            }
        }

        private void ShpViewerGui_KeyUp(object sender, KeyEventArgs e)
        {
            ctrlDown = false;
        }
    }
}
